using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShopPowerTemplate.Entities;
using ShopPowerTemplate.IoC;
using ShopPowerTemplate.Presentation.Mvc.Extensions;
using ShopPowerTemplate.Presentation.Mvc.Models;
using ShopPowerTemplate.Presentation.Mvc.ViewModels;
using ShopPowerTemplate.Services;
using System.Web.Security;
using ShopPowerTemplate.Presentation.Mvc.Providers;

namespace ShopPowerTemplate.Presentation.Mvc.Controllers
{
	public partial class UserController : UserControllerBase
	{
        //Custom code here

        [AcceptVerbs(HttpVerbs.Post)]
        public override ActionResult Create(CreateUserViewModel userModel)
        {
            if (ModelState.IsValid)
            {
                User user = new User();
                AutoMapper.Mapper.Map<CreateUserViewModel, User>(userModel, user);

                //Encrypt password
                user.Password = (Membership.Provider as ShopPowerTemplateMembershipProvider).EncodePassword(user.Password);

				_userService.Add(user);

                return RedirectToAction("Edit", new { id = user.Id});
            }
            else
            {

                return View(userModel);
            }
		}


        [AcceptVerbs(HttpVerbs.Post)]
        public override ActionResult Edit(EditUserViewModel userModel)
        {
            if (ModelState.IsValid)
            {
                User user = _userService.Find(userModel.Id);

                //Change password if needed
                bool passwordChanged = !string.IsNullOrWhiteSpace(userModel.Password);
                string password = user.Password;
                if (passwordChanged)
                    password = (Membership.Provider as ShopPowerTemplateMembershipProvider).EncodePassword(userModel.Password);

                AutoMapper.Mapper.Map<EditUserViewModel, User>(userModel, user);

                //Set password
                user.Password = password;

                _userService.Change(user);

                return RedirectToAction("Index");
            }
            else
            {

                return View(userModel);
            }
        }


        [AcceptVerbs(HttpVerbs.Post)]
        public override ActionResult Load(string sidx, string sord, int page, int rows, string search, int mode = 1)
        {
            string searchValue = this.GetSearchValue();
            OrderBy orderBy = this.GetOrderBy();
            int pageIndex = this.GetPageIndex();
            int pageSize = this.GetPageSize();

            if (mode == (int)JQGridActionEnum.DefaultLoad)
            {
                pageSize = (pageIndex + 1) * pageSize;
                pageIndex = 0;
            }
            else if (mode == (int)JQGridActionEnum.ShowMoreResults)
            {
                pageIndex = page;
                this.SetPageIndex(pageIndex);
            }
            else if (mode == (int)JQGridActionEnum.CustomSearch)
            {
                searchValue = search;
                this.SetSearchValue(searchValue);
                pageIndex = 0;
                this.SetPageIndex(pageIndex);
                orderBy = new OrderBy("", true);
                this.SetOrderBy(orderBy);
            }

            if (!string.IsNullOrEmpty(sidx)) // Sort request
            {
                orderBy = new OrderBy(sidx, sord == "asc");
                this.SetOrderBy(orderBy);
            }

            List<User> searchList = _userService.Search(searchValue, orderBy.Property, orderBy.Ascending, pageIndex, pageSize);
            int count = _userService.SearchCount(searchValue);

            if (!string.IsNullOrEmpty(sidx)) // Sort request
            {
                pageIndex = this.GetPageIndex();
            }

            var data = new
            {
                total = Math.Ceiling((float)count / (float)pageSize),        // Total pages
                page = pageIndex,      // Current page
                records = count,  // Total records
                rows = from user in searchList     // Row data
                       select new
                       {
                           id = user.Id,
                           cell = new string[] {
						   
                                user.Login,
                                user.IsInactive.HasValue && user.IsInactive.Value ? "1" : "0"
								
                           }
                       }
            };

            return Json(data);
        }
	}
}

